package com.schedualnew.mq.util;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;


public class DataTrans {
    private static final String SEP1 = "=";
    private static final String SEP2 = ",";

    public static String ListToString(List<?> list) {
        StringBuffer sb = new StringBuffer();
        if (list != null && list.size() > 0)
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) != null && list.get(i) != "") {


                    if (list.get(i) instanceof List) {
                        sb.append(ListToString((List) list.get(i)));
                        sb.append("=");
                    } else if (list.get(i) instanceof Map) {
                        sb.append(MapToString((Map<?, ?>) list.get(i)));
                        sb.append("=");
                    } else {
                        sb.append(list.get(i));
                        sb.append("=");
                    }
                }
            }
        return sb.toString();
    }


    public static String MapToString(Map<?, ?> map) {
        StringBuffer sb = new StringBuffer();

        for (Object obj : map.keySet()) {
            if (obj == null) {
                continue;
            }
            Object key = obj;
            Object value = map.get(key);
            if (value instanceof List) {
                sb.append(key.toString() + "=" + ListToString((List) value));
                sb.append(",");
                continue;
            }
            if (value instanceof Map) {
                sb.append(key.toString() + "=" + MapToString((Map<?, ?>) value));
                sb.append(",");
                continue;
            }
            sb.append(key.toString() + "=" + value.toString());
            sb.append(",");
        }

        return sb.toString();
    }


    public static Map<String, Object> StringToMap(String mapText) {
        if (mapText == null || mapText.equals("")) {
            return null;
        }

        Map<String, Object> map = new HashMap<>();
        String[] text = mapText.split("\\,");
        for (String str : text) {
            String[] keyText = str.split("=");
            if (keyText.length >= 1) {


                String key = keyText[0];
                if (keyText.length < 2) {
                    map.put(key, "");
                } else {
                    String value = keyText[1];

                    map.put(key, value);
                }
            }
        }
        return map;
    }


    public static Map<String, Object> StringToMap(String mapText, String separator, String separator2) {
        if (mapText == null || mapText.equals("")) {
            return null;
        }

        Map<String, Object> map = new HashMap<>();
        String[] text = StringUtils.split(mapText, separator);
        for (String str : text) {
            String[] keyText = StringUtils.split(str, separator2);
            if (keyText.length >= 1) {


                String key = keyText[0];
                if (keyText.length < 2) {
                    map.put(key, "");
                } else {
                    String value = keyText[1];

                    map.put(key, value);
                }
            }
        }
        return map;
    }


    public static List<Object> StringToList(String listText) {
        if (listText == null || listText.equals("")) {
            return null;
        }
        listText = listText.substring(1);

        List<Object> list = new ArrayList();
        String[] text = listText.split("=");
        for (String str : text) {
            if (str.charAt(0) == 'M') {
                Map<?, ?> map = StringToMap(str);
                list.add(map);
            } else if (str.charAt(0) == 'L') {
                List<?> lists = StringToList(str);
                list.add(lists);
            } else {
                list.add(str);
            }
        }
        return list;
    }


    public static List<String> RemoveRepeat(List<String> lsOld) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < lsOld.size(); i++) {
            if (!list.contains(lsOld.get(i))) {
                list.add(lsOld.get(i));
            }
        }
        return list;
    }


    public static String replaceMsg(String msgPattern, Map<String, String> mapInfo) {
        for (String obj : mapInfo.keySet()) {
            if (obj == null) {
                continue;
            }
            String key = obj;
            String value = mapInfo.get(key);
            if (value == null) {
                value = "";
            }
            String fieldValue = replaceSpecialChar(value.toString());
            String strKey = key.toString().toLowerCase();
            msgPattern = msgPattern.replace("#" + strKey + "#", fieldValue);
            strKey = strKey.toUpperCase();
            msgPattern = msgPattern.replace("#" + strKey + "#", fieldValue);
        }
        return msgPattern;
    }

    public static String replaceSpecialChar(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        }
        str = str.replace("&", "&amp;");
        str = str.replace("<", "&lt;");
        str = str.replace(">", "&gt;");
        str = str.replace("'", "&apos;");
        str = str.replace("\"", "&quot;");
        return str;
    }
}